package com.lsl.myfilemanager.adapter;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lsl.myfilemanager.R;
import com.lsl.myfilemanager.bean.FileInfo;

import java.util.List;

public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.ViewHolder> {

    private List<FileInfo> fileData;

    public FileListAdapter(List<FileInfo> fileData) {
        this.fileData = fileData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View fileItemView = inflater.inflate(R.layout.file_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(fileItemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String fileName = fileData.get(position).getFileName();
        int fileIcon = fileData.get(position).getFileIconResourceId();

        holder.fileName.setText(fileName);
        holder.fileIcon.setImageResource(fileIcon);
    }

    // 视图持有者类
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView fileName;
        public ImageView fileIcon;

        public ViewHolder(View itemView) {
            super(itemView);
            fileName = itemView.findViewById(R.id.fileListName); // 根据实际情况找到对应的视图
            fileIcon = itemView.findViewById(R.id.fileListIcon);
        }
    }

    @Override
    public int getItemCount() {
        return fileData.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<FileInfo> newData) {
        this.fileData = newData;
        notifyDataSetChanged();
    }
}
